<%--
  Created by IntelliJ IDEA.
  User: hp
  Date: 2019/1/6
  Time: 16:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="${pageContext.request.contextPath}/common/tag.jsp"/>
<html>
<head>
    <title>商品销售</title>
</head>
<script>
    $(function () {
        /*生成订单号*/
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var newRandom = "";
        for(var i=0;i<5;i++)
        {
            newRandom += Math.floor(Math.random()*9 + 1).toString();
        }
        var currentdate =  year + month + strDate + newRandom;
        $("#danhao").val(currentdate);
        /*清空要购买的所有商品*/
        $.post("${pageContext.request.contextPath}/xsls/delete",function (data) {})
        /*绑定分类下拉数据*/
        $.post("${pageContext.request.contextPath}/fenlei/queryfenlei",function (data) {
            $.each(data,function (index,item) {
                $("#typeid").append("<option value=\""+item.fid+"\">"+item.fname+"</option>")
                //$("#fenlei").append("<option value=\""+item.fid+"\">"+item.fname+"</option>")
            })
        });
        /*绑定下拉数据*/
        $.post("${pageContext.request.contextPath}/brand/query",function (data) {
            $.each(data,function (index,item) {
                $("#brandid").append("<option value=\""+item.bid+"\">"+item.bname+"</option>")
                //$("#pinpai").append("<option value=\""+item.bid+"\">"+item.bname+"</option>")
            })
        });

        $("#xsnum").blur(function () {
            var id = $("#kid").val();
            var num = $(this).val();
            $.post("${pageContext.request.contextPath}/shopcontrllo/count",{"kid":id,"num":num},function (data) {
                if(data.flag){
                    $("#queding").removeAttr("disabled","disabled");
                }else{
                    $("#queding").attr("disabled","disabled");
                    swal("提示信息", "库存数量不够!", "error");
                }
            });
        });

        $.post("${pageContext.request.contextPath}/worker/query",function (data) {
            $.each(data,function (index,item) {
                $("#ygong").append("<option value=\""+item.yId+"\">"+item.yName+"</option>");
            })
        })

        $("[name='shouji']").blur(function () {
            var pp = $(this).val();
            if(pp.length != 11){
               alert("请输入11为手机号");
                return false;
            } else if(!(/^1(3|4|5|7|8)\d{9}$/.test(pp))) {
                alert("手机号格式不正确");
                return false;
            } else{
                $.post("${pageContext.request.contextPath}/member/getZhekou",{"phone":pp},function (data) {
                    //console.log(data);
                    if(data.zhekou == 0){
                        $("#addhuiyuan").modal('show');
                        $("[name='phone']").val(pp);
                    }else{
                        var ys = $("#yingshou").html();
                        var zhehou = parseFloat(ys*data.zhekou);
                        var youhui = ys-zhehou;
                        $("#zhehou").html(zhehou.toFixed(2));
                        $("#youhui").html(youhui.toFixed(2));

                    }
                })
            }
            $("#sjtishi").html("");
        });

        $("#mai").bootstrapTable({
            url : '',
            columns : [
                {field : 'xssid',title : '商品编号',align : 'center'},
                {field : 'xsname',title : '商品名称',align : 'center'},
                {field : 'xssize',title : '商品尺码',align : 'center'},
                {field : 'brand.bname',title : '商品品牌',align : 'center'},
                {field : 'xsyanse',title : '颜色',align : 'center'},
                {field : 'xsnum',title : '商品数量',align : 'center'},
                {field : 'xsdj',title : '单价',align : 'center'},
                {field : 'xszongjia',title : '总价',align : 'center'},
                {title : '操作',align : 'center',
                    formatter : function (index,row,value) {
                        return "<button class='btn btn-primary'  onclick=\"shanchu("+row.xssid+ ",'"+row.xssize+"')\">删除</button>";
                    }
                },
            ]
        })

        $("#shoptable").bootstrapTable({
            url : '${pageContext.request.contextPath}/kucun/query', //路径
            toolbar : '#add,#pldel',
            pageSize : 8, //设置每页显示的行数
            pageList:[2,4,8,16], //设置可供选择的页面数据条数
            pageNumber:1, //如果设置了分页，首页页码。
            height : 500,  //表格高度
            pagination : true, //开启分页  默认情况下，是客户端分页的
            sidePagination:'server',  //启用服务器端分页
            striped : true,
            queryParams:function(params){
                var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                    limit: params.limit,  //页面大小
                    offset: params.offset, //页码
                    name:$("#shopname").val(),
                    typeid : $("#typeid").val(),
                    sbrand:$("#brandid").val()
                };
                return temp;
            },
            columns : [
                {checkbox : true},
                {field : 'sid',title : '商品编号',align : 'center'},
                {field : 'sname',title : '商品名称',align : 'center'},
                {field : 'ssize',title : '尺码',align : 'center'},
                {field : 'scolor',title : '颜色',align : 'center'},
                {field : 'fenlei.fname',title : '分类',align : 'center'},
                {field : 'brand.bname',title : '品牌',align : 'center'},
                {field : 'goldMoney',title : '进价',align : 'center'},
                {field : 'saleMoney',title : '售价',align : 'center'},
                {field : 'stockNum',title : '库存',align : 'center'}
            ],
            onClickRow : function(row, $element){
                var index = $element.data('index');
                $("#shoptable").bootstrapTable("uncheckAll");
                //console.log(row);
                //选中要修改的行
                $("#shoptable").bootstrapTable("check",index);
                console.log(row);
                $("#bh").html(row.sid);
                $("#mc").html(row.sname);
                $("#fl").html(row.fenlei.fname);
                $("#pp").html(row.brand.bname);
                $("#sj").html(row.saleMoney);
                $("#kc").html(row.stockNum);
                $("#xsdanjia").val(row.saleMoney);
                $("#sinfoModel").modal("show");
                $("#fid").val(row.fenlei.fid);
                $("#bid").val(row.brand.bid);
                $("#cm").html(row.ssize);
                $("#kid").val(row.kid);
                $("#ys").html(row.scolor);
                $.post('${pageContext.request.contextPath}/special/queryinfo',{"sid":row.sid},function (data) {
                    if(data.flag){
                        $("#xsdanjia").val(data.special.spMoeny);
                        $("#xt").html("特价金额：");
                    }else{
                        $("#xt").html("销售金额：");
                    }
                })
            }
        })

        $("#addhy").bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name :{
                    validators: {
                        notEmpty: {
                            message: '请输入姓名'
                        }
                    }
                },
                sex: {
                    validators: {
                        notEmpty: {
                            message: '请选择请别'
                        }
                    }
                },
            }
        })
    })
</script>
<body style="margin: 0px 20px;">
    <div class="">
        <form class="form-inline" style="margin-top: 20px;text-align: right">
            <div class="form-group">
                <label style="font-size: 15px;">商品分类：</label>&nbsp;
                <select name="type" id="typeid" class="form-control">
                    <option value="0">全部</option>
                </select>
            </div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <div class="form-group">
                <label style="font-size: 15px;">品牌：</label>&nbsp;
                <select name="shop" id="brandid" class="form-control">
                    <option value="0">全部</option>
                </select>
            </div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <div class="form-group">
                <label for="shopname">商品名称：</label>&nbsp;
                <input type="text" class="form-control" id="shopname" placeholder="双汇火腿" style="height: 40px;">
                <input type="hidden" id="danhao">
            </div>
            <button onclick="serch();" class="btn btn-primary btn-lg" type="button"><span class="glyphicon glyphicon-search">搜索</span></button>
        </form>
        <table id="shoptable"></table>

        <%--弹框提示要购买东西的信息--%>
        <div class="modal fade" id="sinfoModel">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h2 class="modal-title">添加销售信息</h2>
                    </div>
                    <div class="modal-body">
                        <div>
                            <div style="background: #cccccc;height: 30px;line-height: 30px;padding-left: 30px;"><strong>商品信息</strong></div>
                            <div style="overflow: hidden;margin-top: 10px;">
                                <ul style="list-style: none">
                                    <input type="hidden" id="kid">
                                    <li style="width: 240px;height:30px;float: left;">商品编号： <span id="bh">123456</span></li>
                                    <li style="width: 240px;height:30px;float: left;">商品名称： <span id="mc">123456</span></li>
                                    <li style="width: 240px;height:30px;float: left;">商品尺码： <span id="cm">123456</span></li>
                                    <li style="width: 240px;height:30px;float: left;">商品分类： <span id="fl">123456</span></li>
                                    <li style="width: 240px;height:30px;float: left;">颜色： <span id="ys">123456</span></li>
                                    <input type="hidden" id="fid">
                                    <li style="width: 240px;height:30px;float: left;">商品品牌： <span id="pp">123456</span></li>
                                    <input type="hidden" id="bid">
                                    <li style="width: 240px;height:30px;float: left;">商品售价： <span id="sj">123456</span></li>
                                    <li style="width: 240px;height:30px;float: left;">商品库存： <span id="kc">123456</span></li>
                                </ul>
                            </div>
                        </div>
                        <div>
                            <div style="background: #cccccc;height: 30px;line-height: 30px;padding-left: 30px;"><strong>价格数量</strong></div>
                            <div style="overflow: hidden;margin-top: 10px;">
                                <div class="form-group">
                                    <div style="margin-left: 40px;">
                                        <p style="float: left;margin-top: 3px;" id="xt">销售单价：</p>
                                        <input type="text" id="xsdanjia" class="col-sm-3" disabled="disabled">
                                    </div>
                                    <div style="margin-left: 300px;">
                                        <p style="float: left;margin-top: 3px;">销售数量：</p>
                                        <input type="text" id="xsnum" oninput="myFunction()" value="1" class="col-sm-4">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div>
                            <label style="margin-left: 313px;margin-top: 10px;">总金额：</label> <span id="summoney" style="color:red;">0</span>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" onclick="queding()" id="queding">确定</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
        <%--<a class="btn btn-primary" data-toggle="modal" href="#addhuiyuan">Trigger modal</a>--%>
        <%--添加会员信息--%>
        <div class="modal fade" id="addhuiyuan">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h2 class="modal-title">添加会员</h2>
                    </div>
                    <div class="modal-body">
                        <form action="" id="addhy" class="form-horizontal">
                            <form id="addform" action="" method="post" class="form-horizontal" role="form" enctype="multipart/form-data">
                                <input type="hidden" id="addxiu" value="1">
                                <div class="form-group">
                                    <label class="control-label col-sm-3">姓名：</label>
                                    <div class="col-sm-9">
                                        <input type="text" name="name" class="form-control">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-sm-3">手机号：</label>
                                    <div class="col-sm-9">
                                        <input type="text" name="phone" disabled="disabled" class="form-control">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-sm-3">手机号：</label>
                                    <div class="col-sm-9">
                                        <input type="radio" name="sex" value="男">男 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                        <input type="radio" name="sex" value="女">女
                                    </div>
                                </div>
                            </form>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" onclick="baocun()">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

        <button class="btn btn-primary" style="margin-bottom: 10px;" onclick="qingkong()">删除所有</button>
        <div style="height: 260px;border: 1px solid #cccccc;">
            <table id="mai">
            </table>
        </div>
        <%--下面的金额--%>
            <div style="float: left;width: 100%;margin-top: 10px;">
                <p style="float: left;margin-top: 10px;font-size: 18px;margin-left: 40px;">应收金额： <span style="font-size: 30px;color: red;" id="yingshou">0</span></p>
                <p style="float: left;margin-top: 10px;font-size: 18px;margin-left: 70px;">优惠金额： <span style="font-size: 30px;color: red;" id="youhui">0</span></p>
                <p style="float: left;margin-top: 10px;font-size: 18px;margin-left: 70px;">折后金额： <span style="font-size: 30px;color: red;" id="zhehou">0</span></p>
                <p style="float: left;margin-top: 10px;font-size: 18px;margin-left: 70px;">实收金额： <span style="font-size: 30px;color: red" id="shishou"><input type="text" name="shishou" size="5px;" style="height: 35px;"></span></p>
                <p style="margin-top: 0px;font-size: 19px;margin-left: 950px;">
                    销售员工:
                    <select name="yuangong" id="ygong" style="margin-top: 16px;">
                        <option value="0">请选择员工</option>
                    </select>
                </p>
            </div>
        <%--下面输入的手机号--%>
            <div style="text-align: left;float: left;">
                <p style="font-size: 19px;margin-top: 20px;margin-left: 40px;">手机号： <input type="text" name="shouji"></p>
                <span style="margin-left: 150px;color:red;" id="sjtishi"></span>
            </div>
        <%--下面输入的备注--%>
            <div style="text-align: left;float: left;">
                <p style="font-size: 19px;margin-top: 20px;margin-left: 40px;">备注： <input type="text" size="50px;" id="beizhu"></p>
            </div>
        <%--销售按钮--%>
            <div>
                <button onclick="xiaoshou()" class="btn btn-primary" style="margin-top: 20px;margin-left: 30px;"><span class="glyphicon glyphicon-floppy-disk"></span>销售</button>
            </div>
    </div>
</body>
<script>
    /*点击搜索*/
    function serch() {
        //返回表格的 Options。
        var opts = $("#shoptable").bootstrapTable("getOptions") ;
        var limit = opts.pageSize ;
        var offset = (opts.pageNumber-1) * limit;
        var name = $("#shopname").val() ;
        var typeid = $("#typeid").val();
        var brandid = $("#brandid").val();
        $.post("${pageContext.request.contextPath}/kucun/query",{'name':name,'typeid':typeid,'sbrand':brandid,'limit':limit,'offset':offset},function (data) {
            // var mydata = eval("("+data+")") ;
            //重新绑定表格数据
            $("#shoptable").bootstrapTable('load',data) ;
        })
    }
    /*当模态框弹出时过的数量和单价并赋给总价前*/
    $("#sinfoModel").on('shown.bs.modal',function () {
        var money = $("#xsdanjia").val();
        var num = $("#xsnum").val();
        var sum = money * num;
        $("#summoney").html(sum);
    })

    /*input框发生改变时*/
    function myFunction() {
        var money = $("#xsdanjia").val();
        var num = $("#xsnum").val();
        var sum = money * num;
        $("#summoney").html(sum);
    }
    
    /*确定添加商品*/
    function queding() {
        $("#sinfoModel").modal('hide');
        var danhao = $("#danhao").val();  //单号
        var bh = $("#bh").html(); //商品编号
        var mc = $("#mc").html(); //商品名称
        var bid = $("#bid").val(); //品牌id
        var cm = $("#cm").html(); //尺码
        var ys = $("#ys").html(); //颜色
        var fid = $("#fid").val(); //分类id
        var num = $("#xsnum").val(); //购买数量
        var dj = $("#xsdanjia").val(); //单价
        var moeny = $("#summoney").html(); //总金额

        $.post("${pageContext.request.contextPath}/xsls/insert",{"danhao":danhao,"bh":bh,"mc":mc,"bid":bid,"cm":cm,"ys":ys,"fid":fid,"num":num,"dj":dj,"money":moeny},function (data) {
            //console.log(data);
            $("#mai").bootstrapTable('load',data.xslist);
            $("#yingshou").html(data.zongmoney);
            swal("提示信息", "增加成功!", "success");
        });

    }
    
    function xiaoshou() {
        var yingshou = parseFloat($("#yingshou").html());
        var youhui = parseFloat($("#youhui").html());
        var zhehou = parseFloat($("#zhehou").html());
        var shishou = $("[name='shishou']").val();
        var staffid  = $("[name='yuangong']").val();
        var phone = $("[name='shouji']").val();
        var beizhu = $("#beizhu").val();
        var dingdan = $("#danhao").val();

        if(yingshou == 0){
            swal("提示信息", "请选择销售商品!", "error");
            return false;
        } else if(shishou == ''){
            alert("请输入实收金额");
            return false;
        } else if(shishou<zhehou){
            alert("实收金额要比折扣后金额大");
        }else if(staffid == 0){
            alert("请选择员工");
            return false;
        } else if(phone.length != 11){
            $("#sjtishi").html("请输入11为手机号");
            return false;
        } else if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
            $("#sjtishi").html("手机号格式不正确");
            return false;
        } else{
            $.post("${pageContext.request.contextPath}/xiaoshou/insert",{"yingshou":yingshou,"youhui":youhui,"zhehou":zhehou,"shishou":shishou,"staffid":staffid,"phone":phone,"beizhu":beizhu,"dingdan":dingdan},function (data) {
                console.log(data);
                if(data.chengbai == 1) {
                    /*清空值*/
                    $("#mai").bootstrapTable('removeAll');
                    $("#yingshou").html("0");
                    $("#youhui").html("0");
                    $("#zhehou").html("0");
                    $("[name='shishou']").val("");
                    $("[name='yuangong']").val(0);
                    $("[name='shouji']").val("");
                    $("#beizhu").val("");
                    /*从新生成订单号*/
                    var date = new Date();
                    var year = date.getFullYear();
                    var month = date.getMonth() + 1;
                    var strDate = date.getDate();
                    if (month >= 1 && month <= 9) {
                        month = "0" + month;
                    }
                    if (strDate >= 0 && strDate <= 9) {
                        strDate = "0" + strDate;
                    }
                    var newRandom = "";
                    for (var i = 0; i < 5; i++) {
                        newRandom += Math.floor(Math.random() * 9 + 1).toString();
                    }
                    var currentdate = year + month + strDate + newRandom;
                    $("#danhao").val(currentdate);

                    $("#shoptable").bootstrapTable('load', data);

                    $.post("${pageContext.request.contextPath}/xsls/delete",function (data) {
                        if(data.row > 0){
                            /*$("#mai").bootstrapTable('removeAll');*/ //清空表格数据
                            $("#mai").bootstrapTable('removeAll');
                            $("#yingshou").html("0");
                        }
                    })
                    swal("提示信息", "销售成功!", "success");
                }
            })
        }
    }
    function qingkong() {
        var danhao = $("#danhao").val();
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            /*closeOnConfirm: false*/
        },function () {
            $.post("${pageContext.request.contextPath}/xsls/delete",function (data) {
                if(data.row > 0){
                    /*$("#mai").bootstrapTable('removeAll');*/ //清空表格数据
                    $("#mai").bootstrapTable('removeAll');
                    $("#yingshou").html("0");
                }
                //console.log(data);
            })
        });
    }
    /*单个删除*/
    function shanchu(aa,size) {
        var danhao = $("#danhao").val();
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            /*closeOnConfirm: false*/
        },function () {
            $.post("${pageContext.request.contextPath}/xsls/onedelete",{"dingdan":danhao,"id":aa,"size":size},function (data) {
                //console.log(data);
                if(data.zai == 1){
                    $("#mai").bootstrapTable('load',data.list)
                    $("#yingshou").html(data.qian);
                    swal("提示信息", "删除成功!", "success");
                }else{
                    $("#mai").bootstrapTable('removeAll');
                    $("#yingshou").html("0");
                    //swal("提示信息", "删除成功!", "success");
                }

            });
        });
    }

    function baocun() {
        //启用较验
        $("#addhy").data("bootstrapValidator").validate();
        //验证是否通过true/false
        var flag = $("#addhy").data("bootstrapValidator").isValid() ;
        if(flag){
            var xm = $("[name='name']").val(); //姓名
            var sj = $("[name='phone']").val(); //手机
            var sex = $("input[name='sex']:checked").val(); //性别
            
            $.post("${pageContext.request.contextPath}/member/insertMoRen",{"xm":xm,"sj":sj,"sex":sex},function (data) {
                if(data.count > 0){
                    $("#addhuiyuan").modal('hide');
                    $.post("${pageContext.request.contextPath}/member/getZhekou",{"phone":sj},function (data) {
                        var ys = $("#yingshou").html();
                        var zhehou = parseFloat(ys*data.zhekou);
                        var youhui = ys-zhehou;
                        $("#zhehou").html(zhehou.toFixed(2));
                        $("#youhui").html(youhui.toFixed(2));
                    })
                    swal("提示信息", "增加成功!", "success");
                }
            })
        }
        

    }
</script>
</html>
